/*
 * Created on 2005-01-21
 *
 */
package util.io;


import java.io.File;
import java.util.Hashtable;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ConDat {

	private File plik;
	private Hashtable dane;

	public final static int OK = 0;
	public final static int FILENOTFOUND = 1, READERROR = 2, FORMATERROR = 3;
	public final static int WRITEERROR = 4;
	
	public ConDat(File plik) {
		this.plik = plik;
	}
	
	public int recall() {
		try {
			dane = new Hashtable();
			BufferedReader r = new BufferedReader(new FileReader(plik));
			String ln;
			while ((ln = r.readLine()) != null) {
				if (ln.replaceAll(" ", "").length() == 0) continue;
				if (ln.contains("#")) continue;
				String[] ht = ln.split("=");
				if (ht.length != 2) return FORMATERROR;
				if (dane.contains(ht[0].trim())) return FORMATERROR;
				dane.put(ht[0].trim(), ht[1].trim());
			}
		} catch (FileNotFoundException f) {
			return FILENOTFOUND;
		} catch (IOException i) {
			return READERROR;
		}
		return OK;
	}
	
	public int store() {
		try {
			BufferedWriter w = new BufferedWriter(new FileWriter(plik));
		} catch (IOException i) {
			return WRITEERROR;
		}
		return OK;
	}
	
	public String getValue(String key) {
		return (String) dane.get(key.trim());
	}
	
	public void setValue(String key, String value) {
		dane.put(key.trim(), value.trim());
	}
	
	public void deleteKey(String key) {
		dane.remove(key.trim());
	}
	

}
